package com.tjsinfo.tjsoa.vote.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tjsinfo.tjsoa.Dao.IHQLDao;
import com.tjsinfo.tjsoa.vote.bo.ITVoteColumnBO;
import com.tjsinfo.tjsoa.vote.bo.ITVoteConformationBO;
import com.tjsinfo.tjsoa.vote.bo.ITVoteEstimatedBO;
import com.tjsinfo.tjsoa.vote.vo.TVoteColumn;
import com.tjsinfo.tjsoa.vote.vo.TVoteConformation;
import com.tjsinfo.tjsoa.vote.vo.TVoteEstimated;

public class VoteCakeAction extends Action {
	private IHQLDao hqlDao;
	private ITVoteColumnBO columnBo;
    private ITVoteEstimatedBO estimatedBo;
    private ITVoteConformationBO conformationBo;
	public void setConformationBo(ITVoteConformationBO conformationBo) {
		this.conformationBo = conformationBo;
	}
	public void setEstimatedBo(ITVoteEstimatedBO estimatedBo) {
		this.estimatedBo = estimatedBo;
	}
	public void setColumnBo(ITVoteColumnBO columnBo) {
		this.columnBo = columnBo;
	}
	public void setHqlDao(IHQLDao hqlDao) {
		this.hqlDao = hqlDao;
	}

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		try {
			String headId = request.getParameter("headId");
			String conformationId = request.getParameter("conformationId");
			String parentId = request.getParameter("parentId");
			String estimated = request.getParameter("estimated");
			List<TVoteColumn> columnLists = columnBo.findBy(headId, parentId);

			ArrayList list=new ArrayList();
			List list1;
			List list2;
			
			for (int i = 0; i < columnLists.size(); i++) {
				String sql="select count(*) as num from TVoteValue where estimated='"+estimated+"' and headId='"+headId+"' and valueKey='"+columnLists.get(i).getId()+"' and conformationId='"+conformationId+"'";
				list1 = hqlDao.executeList(sql);
				Long object = (Long) list1.get(0);
				System.out.println(object);
				list.add(object);
				System.out.println(list.get(i));
			}
			
			System.out.println(list.size());
			
			String sql0 = "select count(*) as num1 from TVoteValue where estimated='"+estimated+"' and headId='"+headId+"' and valueKey='0' and conformationId='"+conformationId+"'";
			list2 = hqlDao.executeList(sql0);
			Long object1 = (Long) list2.get(0);
			Integer count0 = Integer.parseInt(object1.toString());
			
			request.setAttribute("list", list);
			request.setAttribute("count0", count0);
			request.setAttribute("columnLists", columnLists);
			
			TVoteEstimated vc = estimatedBo.findById(estimated);
			request.setAttribute("name", vc.getName());
			
			TVoteConformation vcf = conformationBo.findById(conformationId);
			request.setAttribute("conformation", vcf);
			return mapping.findForward("success");
		} catch (Exception e) {
			e.printStackTrace();
			return mapping.findForward("error");
		}
	}
}
